package com.aaa.system.service.imp;

import com.aaa.entity.Dept;
import com.aaa.entity.User;
import com.aaa.system.mapper.DeptMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.DeptService;
import com.aaa.system.service.UserService;
import com.aaa.system.vo.DeptVo;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceImp implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<List<Dept>> findAllDept() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        List<Dept> list = deptMapper.selectList(queryWrapper);
        return new Result<>(200,"查询所有部门",list);
    }

    @Override
    public Result<IPage<Dept>> findListDept(DeptVo deptVo) {
        IPage<Dept> page = new Page(deptVo.getCurrent(),deptVo.getSize());
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(deptVo.getDeptName())){
            wrapper.like("dept_name",deptVo.getDeptName());
        }
        if (Objects.nonNull(deptVo.getStatus())){
            wrapper.eq("status",deptVo.getStatus());
        }
        if (Objects.nonNull(deptVo.getDateRange())&&deptVo.getDateRange().length==2){
            wrapper.between("create_time",deptVo.getDateRange()[0],deptVo.getDateRange()[1]);
        }
        wrapper.eq("del_flag",0);
        IPage<Dept> deptIPage = deptMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询部门成功",deptIPage);
    }

    @Override
    public Result<Boolean> saveOrUpdate(Dept dept) {
        String token = WebUtil.getRequest().getHeader("token");
        String username = TokenUtils.getInfo(token).get("username").toString();
        //根据手机号进行查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",username);
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        User userdata = userMapper.selectOne(queryWrapper);

        //如果id不为空 则为修改
        if (Objects.nonNull(dept.getDeptId())){
            dept.setUpdateTime(LocalDateTime.now());
            dept.setUpdateBy(userdata.getUserName());
            int i = deptMapper.updateById(dept);
            return new Result<>(200,"修改成功",i>0?true:false);
        }else {
            //如果id为空 则为添加
            dept.setCreateTime(LocalDateTime.now());
            dept.setCreateBy(userdata.getUserName());
            int i = deptMapper.insert(dept);
            return new Result<>(200,"添加成功",i>0?true:false);
        }
    }

    @Override
    public Result<Boolean> deleteDept(Long deptId) {
        QueryWrapper<User> wrapper =new QueryWrapper();
        wrapper.eq("dept_id",deptId);
        List<User> userList = userMapper.selectList(wrapper);
        //如果科室下有用户则  不能删除用户
        if (userList.size()>0){
            return new Result<>(200,"该部门下有用户不能被删除",true);
        }
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setDelFlag("1");
        int i = deptMapper.updateById(dept);
        return new Result<>(200,"删除成功",i>0?true:false);
    }

    @Override
    public Result<Boolean> bachDel(Long[] deptIds) {
        List<User> userList = userMapper.selectUserListByDeptIds(deptIds);
        //如果科室下有用户则  不能删除用户
        if (userList.size()>0){
            return new Result<>(200,"某部门下有用户不能被删除",true);
        }
        int i = deptMapper.bachDel(deptIds);
        return new Result<>(200,"删除成功",i>0?true:false);
    }
    /**
     * 远程调用
     *根据部门id查询部门信息
     */
    @Override
    public Dept getAllDeptByDeptId(Long deptId) {
        Dept dept = deptMapper.selectById(deptId);
        return dept;
    }
}
